﻿using System;
using fastCSharp;
using System.IO;

namespace diantou.web.enterprise
{
    /// <summary>
    /// 考试相关调用
    /// </summary>
    [fastCSharp.code.cSharp.webCall]
    class exam : fastCSharp.code.cSharp.webCall.call<exam>
    {
        /// <summary>
        /// 是否ZIP
        /// </summary>
        private bool isResponseZip;
        /// <summary>
        /// 是否支持压缩
        /// </summary>
        protected override bool isGZip { get { return isResponseZip; } }
        /// <summary>
        /// 下载考试成绩报表的Zip
        /// </summary>
        /// <param name="examId"></param>
        [fastCSharp.code.cSharp.webCall]
        public void DownloadReport(int examId, bool isExcel)
        {
            isResponseZip = true;
            diantou.dataAccess.user user = pub.GetSessionUser(this);
            if (user != null && user.IsEnterprisePermission)
            {
                diantou.examDataAccess.tcpCall.exam.downloadReport(examId, isExcel, user.Id, data =>
                {
                    if (data.Type == fastCSharp.net.returnValue.type.Success)
                    {
                        createResponse();
                        try
                        {
                            Response.SetZipContentType();
                            Response.ContentDisposition = ("attachment; filename=51nod_Exam_Report_" + examId.toString() + ".zip").getBytes();
                            response(data.Value);
                            isResponseZip = false;
                        }
                        finally { responseEnd(); }
                    }
                    else serverError500();
                });
            }
            else NotFound404();
        }
        /// <summary>
        /// 下载考试连接
        /// </summary>
        /// <param name="examId"></param>
        [fastCSharp.code.cSharp.webCall]
        public void DownloadReportLink(int examId)
        {
            isResponseZip = true;
            diantou.dataAccess.user user = pub.GetSessionUser(this);
            if (user != null && user.IsEnterprisePermission)
            {
                diantou.examDataAccess.tcpCall.exam.downloadReportLink(examId, user.Id, data =>
                {
                    if (data.Type == fastCSharp.net.returnValue.type.Success)
                    {
                        createResponse();
                        try
                        {
                            Response.SetTextContentType();
                            Response.ContentDisposition = ("attachment; filename=51nod_Exam_ReportLink_" + examId.toString() + ".txt").getBytes();
                            response(data.Value);
                        }
                        finally { responseEnd(); }
                    }
                    else serverError500();
                });
            }
            else NotFound404();
        }
        /// <summary>
        /// 下载考试用户的Zip
        /// </summary>
        /// <param name="examId"></param>
        [fastCSharp.code.cSharp.webCall]
        public void DownloadRegister(int examId, bool isExcel)
        {
            isResponseZip = true;
            diantou.dataAccess.user user = pub.GetSessionUser(this);
            if (user != null && user.IsEnterprisePermission)
            {
                diantou.examDataAccess.tcpCall.exam.downloadRegister(examId, isExcel, user.Id, data =>
                {
                    if (data.Type == fastCSharp.net.returnValue.type.Success)
                    {
                        createResponse();
                        try
                        {
                            Response.SetZipContentType();
                            Response.ContentDisposition = ("attachment; filename=51nod_Exam_Register_" + examId.toString() + ".zip").getBytes();
                            response(data.Value);
                            isResponseZip = false;
                        }
                        finally { responseEnd(); }
                    }
                    else serverError500();
                });
            }
            else NotFound404();
        }
        /// <summary>
        /// 下载考试用户简历
        /// </summary>
        /// <param name="userId"></param>
        [fastCSharp.code.cSharp.webCall]
        public void Resume(int userId)
        {
            isResponseZip = true;
            diantou.examDataAccess.examUser examUser = pub.GetSessionExamUser(this);
            if (examUser == null || examUser.Id != userId)
            {
                examUser = diantou.examDataAccess.clientCache.GetExamUser(userId, pub.GetSessionUser(this));
            }
            if (examUser != null)
            {
                string resume = examUser.Resume;
                if (resume.Length != 0)
                {
                    string fileName = WorkPath + resume;
                    if (File.Exists(fileName))
                    {
                        subString resumeExtensionName = examUser.ResumeExtensionName;
                        createResponse();
                        try
                        {
                            Response.ContentType = fastCSharp.web.contentTypeInfo.GetContentType(ref resumeExtensionName);
                            Response.ContentDisposition = System.Text.Encoding.UTF8.GetBytes("attachment; filename=" + examUser.ResumeName + "." + resumeExtensionName);
                            Response.BodyFile = fileName;
                        }
                        finally { responseEnd(); }
                        return;
                    }
                }
            }
            NotFound404();
        }
    }
}
